package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

import static com.atguigu.lease.model.enums.LeaseStatus.*;

/**
 * Package: com.atguigu.lease.web.admin.schedule
 *
 * @Description:
 * @Author xiongkunYang
 * @Create 2025/4/27 20:38
 */
@Component
public class ScheduledTasks {
    private final LeaseAgreementService leaseAgreementService;

    public ScheduledTasks(LeaseAgreementService leaseAgreementService) {
        this.leaseAgreementService = leaseAgreementService;
    }

    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseStatus() {
        leaseAgreementService
                .update(new LambdaUpdateWrapper<LeaseAgreement>()
                        .le(LeaseAgreement::getLeaseEndDate, new Date())
                        .in(LeaseAgreement::getStatus, SIGNED, WITHDRAWING, RENEWING)
                        .set(LeaseAgreement::getStatus, EXPIRED));
    }
}